package com.crm.service.impl.sys.permissionManagement;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.sys.permissionManagement.SysAccountPermissionMapper;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysAccountPermission;
import com.crm.model.entity.sys.permissionManagement.SysAccountPermissionExample;
import com.crm.service.sys.permissionManagement.SysAccountPermissionDbService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yincuimei
 * @Date 2020/12/6 19:21
 */
@Service
public class SysAccountPermissionDbServiceImpl implements SysAccountPermissionDbService {
    @Autowired
    private SysAccountPermissionMapper sysAccountPermissionMapper;

    /**
     * 增加一条账号-权限组
     * @param sysAccountPermission ---(账号-权限组信息)
     * @return
     */
    @Override
    public Boolean insertSysAccountPermission(SysAccountPermission sysAccountPermission) {
        return sysAccountPermissionMapper.insert(sysAccountPermission) > 0;
    }

    @Override
    public List<SysAccountPermission> selectSysAccountPermissionList(String permissionGroupId, String accountId) {
        SysAccountPermissionExample sysAccountPermissionExample = new SysAccountPermissionExample();
        SysAccountPermissionExample.Criteria criteria = sysAccountPermissionExample.createCriteria();
        if (StringUtils.isNotEmpty(permissionGroupId)) {
            criteria.andPermissionGroupIdEqualTo(permissionGroupId);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }
        if (StringUtils.isNotEmpty(accountId)) {
            criteria.andAccountIdEqualTo(accountId);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }
        return sysAccountPermissionMapper.selectByExample(sysAccountPermissionExample);
    }

    @Override
    public Boolean deleteSysAccountPermissionByPGId(String permissionGroupId, String accountId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        SysAccountPermissionExample sysAccountPermissionExample = new SysAccountPermissionExample();
        SysAccountPermissionExample.Criteria criteria = sysAccountPermissionExample.createCriteria();
        if (StringUtils.isNotEmpty(permissionGroupId)) {
            criteria.andPermissionGroupIdEqualTo(permissionGroupId);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }
        if (StringUtils.isNotEmpty(accountId)) {
            criteria.andAccountIdNotEqualTo(accountId);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }
        SysAccountPermission sysAccountPermission = new SysAccountPermission();
        sysAccountPermission.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        sysAccountPermission.setUpdatePeopleId(userAccount.getAccountId());
        sysAccountPermission.setUpdateTm(new Date());
        return sysAccountPermissionMapper.updateByExampleSelective(sysAccountPermission, sysAccountPermissionExample) > 0;
    }

    @Override
    public List<SysAccountPermission> findPermissionGroupsInfo(String accountId) {
        List<SysAccountPermission> sysAccountPermissions = null;
        if (StringUtils.isNotEmpty(accountId)) {
            SysAccountPermissionExample sysAccountPermissionExample = new SysAccountPermissionExample();
            SysAccountPermissionExample.Criteria criteria = sysAccountPermissionExample.createCriteria();
            criteria.andAccountIdEqualTo(accountId);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            sysAccountPermissions = sysAccountPermissionMapper.selectByExample(sysAccountPermissionExample);
        }
        return sysAccountPermissions;
    }
}
